//表格样式模块  查询数据的展示
//=========================================================
var IMGDOMAIN = "../../images/";


var table_options = {'url': {}, 'cols': {}};
//------------------------------------------------------
var row_numbers = {title: '序号', type: "numbers", fixed: "left"};
var row_survival = {
    field: 'survival', title: '是否可用', align: "center", templet: function (d) {
        return d.survival == 1 ? "可用" : "不可用";
    }
};
var row_survival_other = {
    field: 'survival', title: '是否启用', align: "center", templet: function (d) {
        return d.survival == 1 ? "启用" : "停用";
    }
};

//简单修改按钮
var simple_row_toolbar = {
    title: '操作',
    fixed: 'right',
    width: 150,
    align: 'center',
    toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="update" >编辑</button></div>'
};

//复杂修改按钮
var complex_row_toolbar = {
    title: '操作',
    fixed: 'right',
    width: 150,
    align: 'center',
    toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="complex_update" >编辑</button></div>'
};
//---------------------------------

//=====================================================

table_options.url.admin = domain+"/backstage/superadmin/getadmininpageby_.json";

table_options.cols.admin = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'username', title: '用户名', align: "center"},
    {field: 'role', title: '角色', align: "center"},
    row_survival,
    simple_row_toolbar
]];

//====================================================

table_options.url.company = domain+"/backstage/company/getcompanyinpageby_.json";

table_options.cols.company = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'name', title: '公司名', align: "center"},
    {field: 'address', title: '地址', align: "center"},
    {field: 'consulttel', title: '咨询热线', align: "center"},
    {field: 'advancetel', title: '预约热线', align: "center"},
    {field: 'fax', title: '传真', align: "center"},
    {field: 'email', title: '邮箱', align: "center"},
    {field: 'website', title: '网址', align: "center"},
    {field: 'route', title: '路线', align: "center"},
    {field: 'microblog', title: '微博', align: "center"},
    {field: 'wechat', title: '微信公众号', align: "center"},
    {
        field: 'istop', title: '是否置顶', align: "center", templet: function (d) {
            return d.istop == 1 ? "置顶" : "不置顶";
        }
    },
    row_survival,
    simple_row_toolbar
]];

//=========================================

table_options.url.companyintroduce = domain+"/backstage/company/getcompanyintroduceinpageby_.json";

table_options.cols.companyintroduce = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'title', title: '标题', align: "center"},
    {field: 'content', title: '内容', align: "center"},
    {
        field: 'img', title: '图片', align: "center", templet: function (d) {
            return '<img src="' + IMGDOMAIN + d.img + '" style="width: 220px;height: 70px">';
        }
    },
    row_survival_other,
    complex_row_toolbar
]];

//======================================================================================

table_options.url.navigation = domain+"/backstage/home/getnavigationinpageby_.json";

table_options.cols.navigation = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'serialnumber', title: '栏位', align: "center"},
    {field: 'name', title: '导航名', align: "center"},
    {field: 'href', title: '跳转地址', align: "center"},
    row_survival_other,
    simple_row_toolbar
]];

//====================================================

table_options.url.advertisement = domain+"/backstage/home/getadvertisementinpage.json";

table_options.cols.advertisement = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'adnum', title: '栏位', align: "center"},
    {
        field: 'img', title: '图片', align: "center", templet: function (d) {
            var imgs = d.img.split("!");
            var html = "";
            for (let i of imgs) {
                html += '<img src="' + IMGDOMAIN + i + '" style="width: 120px;height: 90px;margin: 2px">'
            }
            return html;
        }
    },
    {field: 'content', title: '广告内容', align: "center"},
    row_survival_other,
    complex_row_toolbar
]];

//====================================================

table_options.url.productcategory = domain+"/backstage/product/getproductcategoryinpage.json";

table_options.cols.productcategory = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'name', title: '类目名', align: "center"},
    {field: 'describe', title: '描述', align: "center"},
    row_survival,
    simple_row_toolbar
]];

//====================================================

table_options.url.product = domain+"/backstage/product/getproductinpage.json";

table_options.cols.product = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'category_id', title: '类目', hide: true},
    {field: 'category_name', title: '类目名', align: "center"},
    {field: 'name', title: '产品名称', align: "center"},
    {
        field: 'img', title: '图片', align: "center", templet: function (d) {
            return '<img src="' + IMGDOMAIN + d.img + '" style="width: 120px;height: 90px">';
        }
    },
    {field: 'describe', title: '产品简介', align: "center"},
    {
        title: '产品信息',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="product_info_update" >查看编辑</button></div>'
    },
    {
        field: 'onshelf', title: '是否上架', align: "center", templet: function (d) {
            return d.onshelf == 1 ? "上架" : "下架";
        }
    },
    complex_row_toolbar
]];

//======================================================================================

table_options.url.newscategory = domain+"/backstage/news/getnewscategoryinpage.json";

table_options.cols.newscategory = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'name', title: '类目名', align: "center"},
    row_survival,
    simple_row_toolbar
]];

//====================================================

table_options.url.news = domain+"/backstage/news/getnewsinpage.json";

table_options.cols.news = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'category_id', title: '类目', hide: true},
    {field: 'category_name', title: '类目名', align: "center"},
    {field: 'title', title: '新闻标题', align: "center"},
    {field: 'summary', title: '新闻概要', align: "center"},
    {field: 'newsdate', title: '新闻时间', align: "center"},
    {
        title: '新闻信息',
        width: 150,
        align: 'center',
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="news_info_update" >查看编辑</button></div>'
    },
    {
        field: 'survival', title: '是否可用', align: "center", templet: function (d) {
            return d.survival == 1 ? "可用" : "不可用";
        }
    },
    {
        field: 'istop', title: '是否置顶', align: "center", templet: function (d) {
            return d.istop == 1 ? "置顶" : "不置顶";
        }
    },
    complex_row_toolbar
]];


//======================================================================================

table_options.url.message = domain+"/backstage/message/getmessageinpage.json";

table_options.cols.message = [[ //表头
    row_numbers,
    {field: 'id', title: 'ID', hide: true},
    {field: 'name', title: '姓名', align: "center"},
    {field: 'email', title: '邮箱', align: "center"},
    {field: 'message', title: '留言', align: "center"},
]];

//====================================================

//---------------------------------------------------------
function get_render_options(id) {
    return {
        elem: "#" + id, //指定原始表格元素选择器（推荐id选择器）
        url: table_options.url[id],
        where: {"sessionId": sessionId},
        response: {
            statusName: 'status',//规定数据状态的字段名称，默认：code
            statusCode: 200 //规定成功的状态码，默认：0
        },
        page: true,
        limit: 10,
        limits: [10, 15, 20, 25, 30, 35, 40],
        defaultToolbar: [],
        toolbar: "#table_options_toolbar_html_" + id,
        cols: table_options.cols[id],
        //,…… //更多参数参考右侧目录：基本参数选项
    }
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//表单 simple insert 提交事件
var tableToolBarform_insertsubmit_url = {};

//=============================================

tableToolBarform_insertsubmit_url.preadmin = "/superadmin/checkusername.json";
tableToolBarform_insertsubmit_url.admin = "/superadmin/insertadmin.json";

//======================================================

tableToolBarform_insertsubmit_url.company = "/company/insertcompany.json";

//======================================================

tableToolBarform_insertsubmit_url.navigation = "/home/insertnavigation.json";

//======================================================

tableToolBarform_insertsubmit_url.productcategory = "/product/insertproductcategory.json";

//======================================================

tableToolBarform_insertsubmit_url.newscategory = "/news/insertnewscategory.json";

//======================================================

function tableToolBarform_insertsubmit(id, data) {
    if (id == "admin") {
        layer.msg('网络请求中...');
        myajax_backstage(tableToolBarform_insertsubmit_url['pre' + id], 'get', {'username': data.field.username},
            function (result) {
                if (result.status == 200) {
                    myajax_backstage(tableToolBarform_insertsubmit_url[id], 'post', data.field,
                        function (data) {
                            if (data.status == 200) {
                                tableInss[id].reload();
                                layer.msg('新增成功，密码默认为用户名');
                            } else if (data.status == 400) {
                                layer.msg('参数错误，新增失败');
                            } else if (data.status == 500) {
                                layer.msg('服务器错误，新增失败');
                            }
                        },
                        function () {
                            layer.msg("网络请求错误");
                        });
                }
                if (result.status == 500) {
                    layer.msg("用户名已存在，请更换一个更炫酷的名字");
                }
            },
            layer.msg("网络请求错误")
        );
        return false;
    } else {
        layer.msg('网络请求中...');
        myajax_backstage(tableToolBarform_insertsubmit_url[id], 'post', data.field,
            function (result) {
                if (result.status == 200) {
                    tableInss[id].reload();
                    layer.msg('新增成功');
                } else if (result.status == 400) {
                    layer.msg('参数错误，新增失败');
                } else if (result.status == 500) {
                    layer.msg('服务器错误，新增失败');
                }
            },
            function () {
                layer.msg("网络请求错误");
            });
        return false;
    }
}

//==============================================


//弹窗表单 提交新增
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


var tableToolBar_insert_layerform = {"area": {}, "successfunc": {}};

//==================================

tableToolBar_insert_layerform.area.companyintroduce = ['540px', '790px'];


var imgchooseflag_companyintroduce = false;
var layedit_build_options = {
    height: 300,
    tool: [
        '|' //分割线
        , 'strong' //加粗
        , 'italic' //斜体
        , 'underline' //下划线
        , 'del' //删除线
        , '|' //分割线
        , 'left' //左对齐
        , 'center' //居中对齐
        , 'right' //右对齐
        , '|' //分割线
        , 'face' //表情
        , '|' //分割线
    ]
}

var layedit_index_companyintroduce;

tableToolBar_insert_layerform.successfunc.companyintroduce = function (id, layerindex) {

    layedit_index_companyintroduce = layedit.build('content-companyintroduce', layedit_build_options);

    //初始化
    imgchooseflag_companyintroduce = false;
    $('#preview-companyintroduce').attr('src', '');
    layedit.setContent(layedit_index_companyintroduce, '');
    form.val('layer_form_companyintroduce', {
        "title": null,
        "survival": 1
    });

    form.render();

    form.on('submit(submit_' + id + ')', function () {
        if (imgchooseflag_companyintroduce) {
            if (layedit.getContent(layedit_index_companyintroduce).length > 0) {
                layer.msg("网络请求中");
                var insertdata = form.val('layer_form_companyintroduce');
                insertdata.content = layedit.getContent(layedit_index_companyintroduce);
                upLoadImg("#tableToolBar_insert_layerform_html_companyintroduce", function (res) {
                    if (res.status == 200) {
                        insertdata.img = res.src;
                        myajax_backstage("/company/insertcompanyintroduce.json", "post", insertdata, function (result) {
                            if (result.status == 200) {
                                layer.msg("新增成功");
                                tableInss['companyintroduce'].reload();
                                layer.close(layerindex);
                            } else {
                                myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                                layer.msg("新增失败");
                            }
                        }, function () {
                            myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                            layer.msg("提交失败");
                        });
                    } else {
                        layer.msg("图片上传失败")
                    }
                });
            } else {
                layer.msg("请填写内容");
            }
        } else {
            layer.msg("未选择图片");
        }
        return false;
    });
};


//==================================

tableToolBar_insert_layerform.area.advertisement = ['540px', '790px'];

var layedit_index_advertisement;

tableToolBar_insert_layerform.successfunc.advertisement = function (id, layerindex) {

    layedit_index_advertisement = layedit.build('content-advertisement', layedit_build_options);

    //初始化

    form.val('layer_form_advertisement', {
        "adnum": null,
        "survival": 1
    });
    layedit.setContent(layedit_index_advertisement, '');
    form.render();
    input_num_advertisement = 0;
    $('#img-input-advertisement').empty();
    $('#img-insert-show-advertisement').empty();
    $('#img-update-show-advertisement').empty();


    form.on('submit(submit_' + id + ')', function () {
        var insertdata = form.val('layer_form_advertisement');
        insertdata.content = layedit.getContent(layedit_index_advertisement);
        if ($('#img-insert-show-advertisement').children().length > 0) {
            if (insertdata.content.length > 0) {
                upLoadImg("#tableToolBar_insert_layerform_html_advertisement", function (res) {
                    if (res.status == 200) {
                        insertdata.img = res.srcs;
                        myajax_backstage("/home/insertadvertisement.json", "post", insertdata, function (result) {
                            if (result.status == 200) {
                                layer.msg("新增成功");
                                tableInss['advertisement'].reload();
                                layer.close(layerindex);
                            } else {
                                myajax_backstage("/multipledeleteimg.json", "post", {"img": res.srcs});
                                layer.msg("新增失败");
                            }
                        }, function () {
                            myajax_backstage("/multipledeleteimg.json", "post", {"img": res.srcs});
                            layer.msg("提交失败");
                        });
                    } else {
                        layer.msg("图片上传失败")
                    }
                }, null, 1);
            } else {
                layer.msg("请填写内容");
            }
        } else {
            layer.msg("未选择图片");
        }
        return false;
    });
};

//==================================

tableToolBar_insert_layerform.area.product = ['540px', '550px'];


var imgchooseflag_product = false;


tableToolBar_insert_layerform.successfunc.product = function (id, layerindex) {

    //初始化
    imgchooseflag_product = false;
    $('#preview-product').attr('src', '');

    myajax_backstage("/product/getproductcategoryall.json", "get", {}, function (result) {
        if (result.status == 200) {
            $("select[name='category_id']").empty();
            for (let i of result.data) {
                let option = '<option value="' + i.id + '">' + i.name + '</option>';
                $("select[name='category_id']").append(option);
            }
        } else {
            layer.msg("类目获取失败");
        }
        form.val('layer_form_product', {
            "category_id": null,
            "name": null,
            "describe": null,
            "onshelf": 1
        });
        form.render();
    }, function () {
        layer.msg("网络错误");
        layer.close(layerindex);
    });

    form.on('submit(submit_' + id + ')', function () {
        if (imgchooseflag_product) {
            layer.msg("网络请求中");
            var insertdata = form.val('layer_form_product');
            upLoadImg("#tableToolBar_insert_layerform_html_product", function (res) {
                if (res.status == 200) {
                    insertdata.img = res.src;
                    myajax_backstage("/product/insertproduct.json", "post", insertdata, function (result) {
                        if (result.status == 200) {
                            layer.msg("新增成功");
                            tableInss['product'].reload();
                            layer.close(layerindex);
                        } else {
                            myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                            layer.msg("新增失败");
                        }
                    }, function () {
                        myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                        layer.msg("提交失败");
                    });
                } else {
                    layer.msg("图片上传失败")
                }
            });

        } else {
            layer.msg("未选择图片");
        }
        return false;
    });
};


//==================================

tableToolBar_insert_layerform.area.news = ['540px', '530px'];


tableToolBar_insert_layerform.successfunc.news = function (id, layerindex) {
    //初始化
    laydate.render({
        elem: '#news-date' //指定元素
    });

    myajax_backstage("/news/getnewscategoryall.json", "get", {}, function (result) {
        if (result.status == 200) {
            $("select[name='category_id']").empty();
            for (let i of result.data) {
                let option = '<option value="' + i.id + '">' + i.name + '</option>';
                $("select[name='category_id']").append(option);
            }
        } else {
            layer.msg("类目获取失败");
        }
        form.val('layer_form_news', {
            "category_id": null,
            "title": null,
            "summary": null,
            "newsdate": null,
            "survival": 1,
            "istop": 1
        });
        form.render();
    }, function () {
        layer.msg("网络错误");
        layer.close(layerindex);
    });

    form.on('submit(submit_' + id + ')', function () {
        layer.msg("网络请求中");
        var insertdata = form.val('layer_form_news');
        myajax_backstage("/news/insertnews.json", "post", insertdata, function (result) {
            if (result.status == 200) {
                layer.msg("新增成功");
                tableInss['news'].reload();
                layer.close(layerindex);
            } else {
                layer.msg("新增失败");
            }
        }, function () {
            layer.msg("提交失败");
        });
        return false;
    });
};


//==================================


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//监听 表头 工具栏 事件
function selectAndReset(id, obj) {
    switch (obj.event) {
        case 'select':
            var formdata = form.val("toolbar_form_" + id);
            tableInss[id].reload({
                where: form.val("toolbar_form_" + id),
                page: {
                    curr: 1
                },
            });
            form.val("toolbar_form_" + id, formdata);
            break;
        case 'reset':
            var formdata = form.val("toolbar_form_" + id);
            for (var i in formdata) {
                formdata[i] = null;
            }
            form.val("toolbar_form_" + id, formdata);
            tableInss[id].reload({
                where: form.val("toolbar_form_" + id)
            });
            break;
        case 'insert':
            layer.open({
                type: 1,
                title: "新增" + $("a[name=" + id + "]").text(),
                content: $('#tableToolBar_insert_layerform_html_' + id),
                area: tableToolBar_insert_layerform.area[id],
                resize: false,
                success: function (layero, index) {
                    tableToolBar_insert_layerform.successfunc[id](id, index);
                }
            });
            break;
    }
}


//弹窗表单  修改模块
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


var rowToolBar_update_layerform = {
    "idselector": {},
    "area": {},
    "simple_update_url": {},
    "complex_update_successfunc": {}
};

//==================================================

rowToolBar_update_layerform.idselector.admin = '#rowToolBar_update_layerform_html_admin';

rowToolBar_update_layerform.area.admin = ['400px', '420px'];

rowToolBar_update_layerform.simple_update_url.admin = "/superadmin/updateadmin.json";

//===================================================

rowToolBar_update_layerform.idselector.company = '#rowToolBar_update_layerform_html_company';

rowToolBar_update_layerform.area.company = ['400px', '840px'];

rowToolBar_update_layerform.simple_update_url.company = "/company/updatecompany.json";


//==================================================

rowToolBar_update_layerform.idselector.companyintroduce = '#tableToolBar_insert_layerform_html_companyintroduce';

rowToolBar_update_layerform.area.companyintroduce = tableToolBar_insert_layerform.area.companyintroduce;

rowToolBar_update_layerform.complex_update_successfunc.companyintroduce = function (id, layerindex, rowobj) {

    layedit_index_companyintroduce = layedit.build('content-companyintroduce', layedit_build_options);

    var rowdata = rowobj.data;

    //初始化
    imgchooseflag_companyintroduce = false;
    $('#preview-companyintroduce').attr('src', IMGDOMAIN + rowdata.img);
    layedit.setContent(layedit_index_companyintroduce, rowdata.content);
    form.val('layer_form_companyintroduce', rowdata);

    form.render();

    form.on('submit(submit_' + id + ')', function () {
        var updatedata = form.val('layer_form_companyintroduce');
        updatedata.content = layedit.getContent(layedit_index_companyintroduce);
        updatedata.id = rowdata.id;
        if (imgchooseflag_companyintroduce) {
            if (layedit.getContent(layedit_index_companyintroduce).length > 0) {
                layer.msg("网络请求中");
                upLoadImg("#tableToolBar_insert_layerform_html_companyintroduce", function (res) {
                    if (res.status == 200) {
                        updatedata.img = res.src;
                        myajax_backstage("/company/updatecompanyintroduce.json", "post", updatedata, function (result) {
                            if (result.status == 200) {
                                layer.msg("修改成功");
                                tableInss['companyintroduce'].reload();
                                myajax_backstage("/deleteimg.json", "post", {"img": rowdata.img});
                                layer.close(layerindex);
                            } else {
                                myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                                layer.msg("修改失败");
                            }
                        }, function () {
                            myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                            layer.msg("提交失败");
                        })
                    } else {
                        layer.msg("图片上传失败");
                    }
                })
            } else {
                layer.msg("请填写内容");
            }
        } else {
            if (layedit.getContent(layedit_index_companyintroduce).length > 0) {
                myajax_backstage("/company/updatecompanyintroduce.json", "post", updatedata, function (result) {
                    if (result.status == 200) {
                        layer.msg("修改成功");
                        tableInss['companyintroduce'].reload();
                        layer.close(layerindex);
                    } else {
                        layer.msg("修改失败");
                    }
                });
            } else {
                layer.msg("请填写内容");
            }
        }
        return false;
    });
};

//==================================================

rowToolBar_update_layerform.idselector.navigation = '#rowToolBar_update_layerform_html_navigation';

rowToolBar_update_layerform.area.navigation = ['400px', '420px'];

rowToolBar_update_layerform.simple_update_url.navigation = "/home/updatenavigation.json";

//==================================================

rowToolBar_update_layerform.idselector.advertisement = '#tableToolBar_insert_layerform_html_advertisement';

rowToolBar_update_layerform.area.advertisement = tableToolBar_insert_layerform.area.advertisement;

rowToolBar_update_layerform.complex_update_successfunc.advertisement = function (id, layerindex, rowobj) {

    layedit_index_advertisement = layedit.build('content-advertisement', layedit_build_options);

    var rowdata = rowobj.data;

    //初始化
    layedit.setContent(layedit_index_advertisement, rowdata.content);
    form.val('layer_form_advertisement', rowdata);
    form.render();

    input_num_advertisement = 0;
    $('#img-input-advertisement').empty();
    $('#img-insert-show-advertisement').empty();
    $('#img-update-show-advertisement').empty();

    for (let i of rowdata.img.split("!")) {
        var html = '<img style="width: 120px;height: 90px;margin: 2px" ondblclick="delete_img_advertisement(this)" src="' + IMGDOMAIN + i + '" >';
        $('#img-update-show-advertisement').append(html);
    }

    form.on('submit(submit_' + id + ')', function () {
        var updatedata = form.val('layer_form_advertisement');
        updatedata.content = layedit.getContent(layedit_index_advertisement);
        updatedata.id = rowdata.id;
        updatedata.img = "";
        var delete_img = rowdata.img;
        if ($('#img-insert-show-advertisement').children().length > 0) {
            if (updatedata.content.length > 0) {
                layer.msg("网络请求中");
                upLoadImg("#tableToolBar_insert_layerform_html_advertisement", function (res) {
                    if (res.status == 200) {
                        updatedata.img = res.srcs;
                        creat_update_date_advertisement(updatedata, delete_img, layerindex);
                    } else {
                        layer.msg("图片上传失败");
                    }
                }, null, 1)
            } else {
                layer.msg("请填写内容");
            }
        } else {
            if ($('#img-update-show-advertisement').children().length > 0) {
                if (updatedata.content.length > 0) {
                    creat_update_date_advertisement(updatedata, delete_img, layerindex);
                } else {
                    layer.msg("请填写内容");
                }
            } else {
                layer.msg("请上传图片");
            }
        }
        return false;
    });
};

function creat_update_date_advertisement(updatedata, delete_img, layerindex) {
    var old_imgs = "";
    for (let i of $('#img-update-show-advertisement').children()) {
        let path = $(i).attr("src").substring(IMGDOMAIN.length);
        old_imgs += "!" + path;
        delete_img = delete_img.replace(path, "");
    }
    updatedata.img = old_imgs + updatedata.img;
    updatedata.img = updatedata.img.substring(1);
    myajax_backstage("/home/updateadvertisement.json", "post", updatedata, function (result) {
        if (result.status == 200) {
            layer.msg("修改成功");
            tableInss['advertisement'].reload();
            myajax_backstage("/multipledeleteimg.json", "post", {"img": delete_img});
            layer.close(layerindex);
        } else {
            layer.msg("修改失败");
        }
    });
}

//===================================================

rowToolBar_update_layerform.idselector.productcategory = '#rowToolBar_update_layerform_html_productcategory';

rowToolBar_update_layerform.area.productcategory = ['400px', '360px'];

rowToolBar_update_layerform.simple_update_url.productcategory = "/product/updateproductcategory.json";

//==================================================

rowToolBar_update_layerform.idselector.product = '#tableToolBar_insert_layerform_html_product';

rowToolBar_update_layerform.area.product = tableToolBar_insert_layerform.area.product;

rowToolBar_update_layerform.complex_update_successfunc.product = function (id, layerindex, rowobj) {

    var rowdata = rowobj.data;

    //初始化
    imgchooseflag_product = false;
    $('#preview-product').attr('src', IMGDOMAIN + rowdata.img);
    myajax_backstage("/product/getproductcategoryall.json", "get", {}, function (result) {
        if (result.status == 200) {
            $("select[name='category_id']").empty();
            for (let i of result.data) {
                let option = '<option value="' + i.id + '">' + i.name + '</option>';
                $("select[name='category_id']").append(option);
            }
        } else {
            layer.msg("类目获取失败");
        }
        form.val('layer_form_product', rowdata);
        form.render();
    }, function () {
        layer.msg("网络错误");
        layer.close(layerindex);
    });

    form.on('submit(submit_' + id + ')', function () {
        var updatedata = form.val('layer_form_product');
        updatedata.id = rowdata.id;
        if (imgchooseflag_product) {
            layer.msg("网络请求中");
            upLoadImg("#tableToolBar_insert_layerform_html_product", function (res) {
                if (res.status == 200) {
                    updatedata.img = res.src;
                    myajax_backstage("/product/updateproduct.json", "post", updatedata, function (result) {
                        if (result.status == 200) {
                            layer.msg("修改成功");
                            tableInss['product'].reload();
                            myajax_backstage("/deleteimg.json", "post", {"img": rowdata.img});
                            layer.close(layerindex);
                        } else {
                            myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                            layer.msg("修改失败");
                        }
                    }, function () {
                        myajax_backstage("/deleteimg.json", "post", {"img": res.src});
                        layer.msg("提交失败");
                    })
                } else {
                    layer.msg("图片上传失败");
                }
            })
        } else {
            myajax_backstage("/product/updateproduct.json", "post", updatedata, function (result) {
                if (result.status == 200) {
                    layer.msg("修改成功");
                    tableInss['product'].reload();
                    layer.close(layerindex);
                } else {
                    layer.msg("修改失败");
                }
            });
        }
        return false;
    });
};

//==================================================

rowToolBar_update_layerform.idselector.newscategory = '#rowToolBar_update_layerform_html_newscategory';

rowToolBar_update_layerform.area.newscategory = ['400px', '300px'];

rowToolBar_update_layerform.simple_update_url.newscategory = "/news/updatenewscategory.json";

//===================================================

rowToolBar_update_layerform.idselector.news = '#tableToolBar_insert_layerform_html_news';

rowToolBar_update_layerform.area.news = tableToolBar_insert_layerform.area.news;

rowToolBar_update_layerform.complex_update_successfunc.news = function (id, layerindex, rowobj) {

    var rowdata = rowobj.data;
    //初始化
    laydate.render({
        elem: '#news-date' //指定元素
    });

    myajax_backstage("/news/getnewscategoryall.json", "get", {}, function (result) {
        if (result.status == 200) {
            $("select[name='category_id']").empty();
            for (let i of result.data) {
                let option = '<option value="' + i.id + '">' + i.name + '</option>';
                $("select[name='category_id']").append(option);
            }
        } else {
            layer.msg("类目获取失败");
        }
        form.val('layer_form_news', rowdata);
        form.render();
    }, function () {
        layer.msg("网络错误");
        layer.close(layerindex);
    });

    form.on('submit(submit_' + id + ')', function () {
        layer.msg("网络请求中");
        var updatedata = form.val('layer_form_news');
        updatedata.id = rowdata.id;
        myajax_backstage("/news/updatenews.json", "post", updatedata, function (result) {
            if (result.status == 200) {
                layer.msg("修改成功");
                tableInss['news'].reload();
                layer.close(layerindex);
            } else {
                layer.msg("修改失败");
            }
        }, function () {
            layer.msg("提交失败");
        });
        return false;
    });
};

//===================================================

var layedit_index_productinfo_description;
var layedit_index_productinfo_sendinfo;
var layedit_index_productinfo_cqcinfo;
var layedit_index_productinfo_packageinfo;

var layedit_build_options_productinfo = {
    height: 100,
    tool: []
}

var layedit_index_newsinfo_content;

var layedit_build_options_newsinfo = {
    height: 300,
    tool: [
        '|' //分割线
        , '|' //分割线
        , '|' //分割线
        , 'strong' //加粗
        , '|' //分割线
        , 'italic' //斜体
        , '|' //分割线
        , 'underline' //下划线
        , '|' //分割线
        , 'del' //删除线
        , '|' //分割线
        , '|' //分割线
        , '|' //分割线
        , 'left' //左对齐
        , '|' //分割线
        , 'center' //居中对齐
        , '|' //分割线
        , 'right' //右对齐
        , '|' //分割线
        , '|' //分割线
        , '|' //分割线
        , 'face' //表情
        , '|' //分割线
        , '|' //分割线
        , '|' //分割线
    ]
};

//监听行工具条
function rowToolBar(id, obj) {
    switch (obj.event) {
        case 'update':
            layer.open({
                type: 1,
                title: "修改" + $("a[name=" + id + "]").text(),
                content: $(rowToolBar_update_layerform.idselector[id]),
                area: rowToolBar_update_layerform.area[id],
                resize: false,
                success: function (layero, index) {
                    form.val(id + '_updateform', obj.data);
                    form.render();
                    form.on('submit(' + id + '_update)', function () {
                        var rawdata = obj.data;
                        var updatedata = form.val(id + '_updateform');
                        updatedata.id = rawdata.id;
                        myajax_backstage(rowToolBar_update_layerform.simple_update_url[id], "post", updatedata, function (result) {
                            if (result.status == 200) {
                                tableInss[id].reload();
                                layer.close(index);
                                layer.msg('修改成功');
                            } else {
                                layer.msg('修改失败');
                            }
                        }, function () {
                            layer.msg('网络错误，修改失败');
                        });
                        return false;
                    });
                }
            });
            break;
        case 'complex_update':
            layer.open({
                type: 1,
                title: "修改" + $("a[name=" + id + "]").text(),
                content: $(rowToolBar_update_layerform.idselector[id]),
                area: rowToolBar_update_layerform.area[id],
                resize: false,
                success: function (layero, index) {
                    rowToolBar_update_layerform.complex_update_successfunc[id](id, index, obj);
                }
            });
            break;
        case 'product_info_update':
            layer.open({
                type: 1,
                title: "修改产品介绍",
                content: $("#rowToolBar_update_layerform_html_productinfo"),
                area: ['900px', '640px'],
                resize: false,
                success: function (layero, index) {
                    var rowdata = obj.data;
                    var isnull = false;
                    //初始化
                    layedit_index_productinfo_description = layedit.build('product-info-description', layedit_build_options_productinfo);
                    layedit_index_productinfo_sendinfo = layedit.build('product-info-sendinfo', layedit_build_options_productinfo);
                    layedit_index_productinfo_cqcinfo = layedit.build('product-info-cqcinfo', layedit_build_options_productinfo);
                    layedit_index_productinfo_packageinfo = layedit.build('product-info-packageinfo', layedit_build_options_productinfo);
                    myajax_backstage("/product/getproductinfobyproduct_id.json", "get", {"product_id": rowdata.id}, function (result) {
                        if (result.status == 200) {
                            if (result.data != null) {
                                form.val("productinfo_updateform", {
                                    "price": result.data.price / 100,
                                    "listprice": result.data.listprice / 100,
                                    "payment": result.data.payment,
                                    "survival": result.data.survival
                                });
                                layedit.setContent(layedit_index_productinfo_description, result.data.description);
                                layedit.setContent(layedit_index_productinfo_sendinfo, result.data.sendinfo);
                                layedit.setContent(layedit_index_productinfo_cqcinfo, result.data.cqcinfo);
                                layedit.setContent(layedit_index_productinfo_packageinfo, result.data.packageinfo);
                            } else {
                                isnull = true;
                                form.val("productinfo_updateform", {
                                    "price": '',
                                    "listprice": '',
                                    "payment": '',
                                    "survival": ''
                                });
                                layedit.setContent(layedit_index_productinfo_description, "");
                                layedit.setContent(layedit_index_productinfo_sendinfo, "");
                                layedit.setContent(layedit_index_productinfo_cqcinfo, "");
                                layedit.setContent(layedit_index_productinfo_packageinfo, "");
                            }
                        } else {
                            layer.msg("服务器错误");
                        }
                        form.render();
                    }, function () {
                        layer.msg("网络错误");
                    });
                    var success = function (result) {
                        if (result.status == 200) {
                            layer.msg("编辑成功");
                            layer.close(index);
                        } else {
                            layer.msg("编辑失败");
                        }
                    };
                    form.on('submit(product_info)', function (data) {
                        var optionsdata = {};
                        var formdate = form.val("productinfo_updateform");
                        optionsdata.product_id = rowdata.id;
                        optionsdata.price = parseInt(parseFloat(formdate.price) * 100);
                        optionsdata.listprice = parseInt(parseFloat(formdate.listprice) * 100);
                        optionsdata.description = layedit.getContent(layedit_index_productinfo_description);
                        optionsdata.sendinfo = layedit.getContent(layedit_index_productinfo_sendinfo);
                        optionsdata.cqcinfo = layedit.getContent(layedit_index_productinfo_cqcinfo);
                        optionsdata.packageinfo = layedit.getContent(layedit_index_productinfo_packageinfo);
                        optionsdata.payment = formdate.payment;
                        optionsdata.survival = formdate.survival;
                        layer.msg("网络请求中...");
                        if (isnull) {
                            myajax_backstage("/product/insertproductinfo.json", "post", optionsdata, success, function () {
                                layer.msg("网络错误");
                            });
                        } else {
                            myajax_backstage("/product/updateproductinfo.json", "post", optionsdata, success, function () {
                                layer.msg("网络错误");
                            });
                        }
                        return false;
                    });
                }
            });
            break;
        case 'news_info_update':
            layer.open({
                type: 1,
                title: "修改产品介绍",
                content: $("#rowToolBar_update_layerform_html_newsinfo"),
                area: ['800px', '640px'],
                resize: false,
                success: function (layero, index) {
                    var rowdata = obj.data;
                    var isnull = false;
                    //初始化
                    layedit_index_newsinfo_content = layedit.build('news-info-content', layedit_build_options_newsinfo);

                    myajax_backstage("/news/getnewsinfobyproduct_id.json", "get", {"news_id": rowdata.id}, function (result) {
                        if (result.status == 200) {
                            if (result.data != null) {
                                form.val("newsinfo_updateform", result.data);
                                layedit.setContent(layedit_index_newsinfo_content, result.data.content);
                            } else {
                                isnull = true;
                                form.val("newsinfo_updateform", {
                                    "source": '',
                                    "author": '',
                                    "numviews": '',
                                    "survival": ''
                                });
                                layedit.setContent(layedit_index_newsinfo_content, "");
                            }
                        } else {
                            layer.msg("服务器错误");
                        }
                        form.render();
                    }, function () {
                        layer.msg("网络错误");
                    });
                    var success = function (result) {
                        if (result.status == 200) {
                            layer.msg("编辑成功");
                            layer.close(index);
                        } else {
                            layer.msg("编辑失败");
                        }
                    };
                    form.on('submit(news_info)', function (data) {
                        var optionsdata = form.val("newsinfo_updateform");
                        optionsdata.news_id = rowdata.id;
                        optionsdata.content = layedit.getContent(layedit_index_newsinfo_content);
                        layer.msg("网络请求中...");
                        if (isnull) {
                            myajax_backstage("/news/insertnewsinfo.json", "post", optionsdata, success, function () {
                                layer.msg("网络错误");
                            });
                        } else {
                            myajax_backstage("/news/updatenewsinfo.json", "post", optionsdata, success, function () {
                                layer.msg("网络错误");
                            });
                        }
                        return false;
                    });
                }
            });
            break;
    }
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null;
    // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

// //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>